import { request } from '@/utils/request.js';

const { protocol } = document.location;
const baseURL = import.meta.env.VITE_API_BASE_URL;
const basePATH = import.meta.env.VITE_API_BASE_PATH;
const url = '/adminapi/common/';

/**
 * 文件上传APi
 */
export function uploadURL() {
  return `${protocol}${baseURL}${url}upload`;
}

/**
 * 文件下载地址
 */
export function downloadPath() {
  return `${protocol}${basePATH}uploads/`;
}

/**
 * 验证码
 */
export function apiCaptcha(params) {
  return request({
    url: `${url}captcha`,
    method: 'get',
    params: params,
  });
}

/**
 * 文件上传
 */
export function apiUpload(data) {
  return request({
    url: `${url}upload`,
    method: 'post',
    data: data,
    timeout: 30000,
    headers: {'Content-Type': 'multipart/form-data'},
  });
}

/**
 * 文件下载
 */
export function apiDownload(data) {
  return request({
    url: `${url}download`,
    method: 'post',
    data: data,
  });
}

/**
 * 设置
 */
export function apiSetting(data, method = 'get') {
  if (method === 'get') {
    return request({
      url: `${url}setting`,
      method: 'get',
      params: data,
    });
  }
  return request({
    url: `${url}setting`,
    method: 'post',
    data: data,
  });
}

/**
 * 清除缓存
 */
export function apiClearCache(params = {}) {
  return request({
    url: `${url}delCache`,
    method: 'get',
    params: params,
  });
}

/**
 * 省市区
 */
export function apiRegions(params = {}) {
  return request({
    url: `${url}getRegion`,
    method: 'get',
    params: params,
  });
}

export function download(url, method = 'post') {
  return request({
    url,
    method,
    responseType: 'blob',
  });
}
